﻿using B.S.BasicData.Domain.XU;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.Xu;
using B.S.BasicData.Write.Api.Dto;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.Xu
{
    /// <summary>
    /// 箱子查询
    /// </summary>
    public class BoxQueryHandler : IRequestHandler<BoxQueryCommand, ApiPaging<BoxListDto>>
    {
        private readonly IBaseRepository<BoxModel> _boxRepository;

        public BoxQueryHandler(IBaseRepository<BoxModel> boxRepository)
        {
            _boxRepository = boxRepository;
        }
        public Task<ApiPaging<BoxListDto>> Handle(BoxQueryCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<BoxListDto> result = new ApiPaging<BoxListDto>();

            var boxlists = _boxRepository.GetAll();
            if (!string.IsNullOrEmpty(request.BoxName))
            {
                boxlists = boxlists.Where(p => p.BoxName.Contains(request.BoxName));
            }
            var linq = from a in boxlists
                       select new BoxListDto
                       {
                           Id = a.Id,
                           BoxName = a.BoxName,
                           Height = a.Height,
                           Width = a.Width,
                           length = a.length,
                           Remark = a.Remark,
                           State = a.State,

                       };
            result.TotalCount= linq.Count();
            result.TotalPage=(int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            result.Items = linq.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Code = ApiEnums.Success;
            result.Msg = "查询成功";
            return Task.FromResult(result);
        }
    }
}
